package main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

//Nikolas Wolfe
//3/18/09
//CIS 4930
//Game Of Life

// This manages the dialog box for the number of Threads/Generations

public class DialogBox
{
	private final FrameOfLife context;
	private final JDialog dialog;
	private final String dialogType;
	private JButton btnOk;
	private JButton btnCancel;
	private JTextField txtArea;
	private ActionListener okListener;
	private ActionListener cancelListener;
	private Integer returnVal;
	
	public DialogBox( int initialVal, String name, FrameOfLife frame )
	{
		this.returnVal = initialVal;
		this.dialogType = name;
		this.context = frame;
		this.dialog = new JDialog( context );
	}
	
	//==========================================================================
	// createDialog
	
	public void initialize()
	{	
		if( dialog == null )
			throw new IllegalStateException("Dialog not initialized!");
		
		Dimension dim = new Dimension(200, 100);
		dialog.setPreferredSize(dim);

		btnOk = new JButton("OK");
		btnCancel = new JButton("Cancel");
		
		JPanel btnPanel = new JPanel();
		btnPanel.add(btnOk, BorderLayout.WEST);
		btnPanel.add(btnCancel, BorderLayout.EAST);

		txtArea = new JTextField("0", 11);
		JPanel txtPanel = new JPanel();
		txtPanel.add( txtArea, BorderLayout.CENTER );
		
		dialog.getContentPane().add(txtPanel, BorderLayout.NORTH);
		dialog.getContentPane().add(btnPanel, BorderLayout.SOUTH);
		dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

		dialog.pack();
		dialog.setTitle("Enter # " + dialogType );
		
		okListener = new ActionListener()
		{
			public void actionPerformed(ActionEvent event) 
			{
				try
				{
					returnVal = Integer.parseInt(txtArea.getText());
				}
				catch( NumberFormatException e )
				{
					// user entered babble, default to zero
					returnVal = 0;
				}
				dialog.setVisible( false );
			}
		};
		
		cancelListener = new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				dialog.setVisible( false );
			}
		};
		registerListeners();
	}
	
	//==========================================================================
	// show()
	
	public void show()
	{
		if( dialog == null )
			throw new IllegalStateException( "DialogBox not initialized!" );
		dialog.setVisible( true );
	}
	
	//==========================================================================
	// hide()
	
	public void hide()
	{
		if( dialog == null )
			throw new IllegalStateException( "DialogBox not initialized!" );
		dialog.setVisible( false );
	}
	
	//==========================================================================
	// registerListeners()
	
	private void registerListeners()
	{
		if( okListener == null || cancelListener == null )
			throw new IllegalStateException( "Listeners not initialized!" );
		btnOk.addActionListener( okListener );
		btnCancel.addActionListener( cancelListener );
	}
	
	//==========================================================================
	// unregisterListeners()
	
	public void unregisterListeners()
	{
		if( okListener == null || cancelListener == null )
			throw new IllegalStateException( "Listeners not initialized!" );
		btnOk.removeActionListener( okListener );
		btnCancel.removeActionListener( cancelListener );
	}
	
	//==========================================================================
	// getVal() -- returns the result of the most recent dialog box interaction
	
	public int getVal()
	{
		return this.returnVal;
	}
}
